package com.yuyou.fn.controller;

import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.educational.entity.Room;
import com.yuyou.fn.educational.service.IRoomService;
import com.yuyou.fn.platform.entity.SysUser;
import com.yuyou.fn.tradingservice.entity.*;
import com.yuyou.fn.tradingservice.service.*;
import com.yuyou.fn.tradingservice.vo.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by itachi on 2017/04/05.
 */
@Controller
@RequestMapping("/api/admin/trading")
public class AdminTradingController extends AdminApiBaseController{

    @Resource
    private IServiceCategoryService categoryService;

    @Resource
    private IServiceOrderService orderService;

    @Resource
    private IServiceOrderItemService orderItemService;

    @Resource
    private IServiceProductService productService;

    @Resource
    private IServiceProductRefundService productRefundService;

    @Resource
    private IShippingAddressService addressService;

    @Resource
    private IServiceMerchantService merchantService;

    @Resource
    private IServiceImageService imageService;

    @Resource
    private IServiceCommentService commentService;

    @Resource
    private IRoomService roomService ;

    @ApiOperation("保存分类信息")
    @RequestMapping(value = "/saveCategory", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveCategory(ServiceCategoryVo categoryVo) {
        categoryService.saveCategory(categoryVo);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("更新分类信息")
    @RequestMapping(value = "/editCategory", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> editCategory(ServiceCategoryVo categoryVo) {
        categoryService.editCategory(categoryVo);
        return ReturnResult.success("ok");
    }

    @ApiOperation("删除分类信息")
    @RequestMapping(value = "/deleteCategory", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> deleteCategory(@RequestParam("categoryId") Long categoryId) {
        categoryService.deleteById(categoryId);
        productService.deleteProduct(categoryId);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("查看该分类是否有子孙类")
    @RequestMapping(value = "/getChildCategory", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ReturnResult<Void> getChildCategory(@RequestParam("categoryId") Long categoryId) {
        List<ServiceCategory> categoryList =  categoryService.getChildCategory(categoryId);
        if(categoryList.size() > 0) {
            return ReturnResult.success("该分类下还有分类");
        } else {
            List<ServiceProduct> productList = productService.findProductsByCategoryId(categoryId);
            if(productList.size() > 0) {
                return ReturnResult.success("该分类下还有产品，您确定是否删除");
            } else {
                return ReturnResult.success("是否确定删除");
            }
        }
    }

    @ApiOperation("分类详情")
    @RequestMapping(value = "/getCategory", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<ServiceCategory> getCategory(@RequestParam("categoryId") Long categoryId) {
        ServiceCategory category = categoryService.findById(categoryId);
        return ReturnResult.success(category);
    }

    @ApiOperation("分类列表")
    @RequestMapping(value = "/showCategory", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<ServiceCategory>> showCategory(ServiceCategoryVo categoryVo, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "15") int pageSize) {
        Page<ServiceCategory> page = categoryService.findCategoryForPage(categoryVo, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("获取所有分类信息")
    @RequestMapping(value = "/getAllCategory", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<ServiceCategory>> getAllCategory(@RequestParam(value = "type") String type) {
        Integer intType = Integer.parseInt(type);
        Example<ServiceCategory> example = ServiceCategory.createExample();
        example.createCriteria().andEqualTo("type",intType);
        List<ServiceCategory> list = categoryService.findListByExample(example);
        return ReturnResult.success(list);
    }

    @ApiOperation("分类信息")
    @RequestMapping(value = "/getCategoryMessage", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<ServiceCategory>> getCategoryMessage() {
        Example<ServiceCategory> example = ServiceCategory.createExample();
        List<ServiceCategory> list = categoryService.findListByExample(example);
        return ReturnResult.success(list);
    }

    @ApiOperation("获取商品信息")
    @RequestMapping(value = "/getServiceProduct", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<ServiceProduct> getServiceProduct(@RequestParam("productId") Long productId) {
        ServiceProduct product = productService.findById(productId);
        return ReturnResult.success(product);
    }

    @ApiOperation("获取商品图片")
    @RequestMapping(value = "/productImageList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<String[]> productImageList(@RequestParam("productId") Long productId) {
        String[] list = imageService.findUrlArrayByProductId(productId);
        return ReturnResult.success(list);
    }

    @ApiOperation("保存商品")
    @RequestMapping(value = "/saveServiceProduct", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveServiceProduct(ServiceProductVo productVo,String productImages, HttpServletRequest request) {
        SysUser user = getUser(request);
        productVo.setBusTeamId(user.getBusTeamId());
        productVo.setUserId(user.getUserId());
        productVo.setUsername(user.getUsername());
        productService.saveServiceProduct(productVo,productImages);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("商品列表")
    @RequestMapping(value = "/showServiceProduct", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<ServiceProduct>> showServiceProduct(ServiceProductVo productVo, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize, @RequestParam("type") int type) {
        productVo.setType(type);
        Page<ServiceProduct> page = productService.findProductForPage(productVo, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("删除商品")
    @RequestMapping(value = "/deleteServiceProduct", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> deleteServiceProduct(@RequestParam(value = "productId") Long productId) {
        productService.deleteById(productId);
        return ReturnResult.success("Ok");
    }


    @ApiOperation("获取服务信息")
    @RequestMapping(value = "/getServiceProductManage", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<ServiceProduct> getServiceProductManage(@RequestParam("productId") Long productId) {
        ServiceProduct product = productService.findById(productId);
        return ReturnResult.success(product);
    }

    @ApiOperation("服务管理列表")
    @RequestMapping(value = "/showServiceProductManage", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<ServiceProduct>> showServiceProductManage(ServiceProductVo productVo, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {

        Page<ServiceProduct> page = productService.findProductForPage(productVo, pageNo, pageSize);

        return ReturnResult.success(page);
    }


    @ApiOperation("产品退费申请列表")
    @RequestMapping(value = "/productRefundList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<ServiceProductRefund>> productRefundList(ProductRefundQueryVo productRefundQueryVo, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,HttpServletRequest request) {
        SysUser user = getUser(request);
        productRefundQueryVo.setSellerId(user.getBusTeamId());
        Page<ServiceProductRefund> page = productRefundService.findSeviceRefundPage(productRefundQueryVo,pageNo,pageSize);
        return ReturnResult.success(page);
    }


    @ApiOperation("产品退费申请详情")
    @RequestMapping(value = "/productRefundDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<ServiceProductRefund> productRefundDetail(@RequestParam(value = "serviceProductRefundId") Long serviceProductRefundId) {
        ServiceProductRefund serviceProductRefund = productRefundService.findById(serviceProductRefundId);
        return ReturnResult.success(serviceProductRefund);
    }

    @ApiOperation("新增或者审批产品退费申请")
    @RequestMapping(value = "/saveOrUpdateProductRefund", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateProductRefund(ProductRefundQueryVo productRefundQueryVo,HttpServletRequest request) {
        SysUser user = getUser(request);
        productRefundQueryVo.setUserId(user.getUserId());
        productRefundQueryVo.setUserName(user.getRealName());
        productRefundService.saveOrUpdateProductRefundDetail(productRefundQueryVo);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("删除商品退费申请")
    @RequestMapping(value = "/deleteProductRefund", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> deleteProductRefund(@RequestParam("serviceProductRefundId") Long serviceProductRefundId) {
        productRefundService.deleteById(serviceProductRefundId);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("根据orderItemId找商品退费详情")
    @RequestMapping(value = "/getOrderItemRefund", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<ServiceProductRefund> getOrderItemRefund(@RequestParam(value = "orderItemId") Long orderItemId) {
        ServiceProductRefund refund = productRefundService.getOrderItemRefundDetail(orderItemId);
        return ReturnResult.success(refund);
    }


    @ApiOperation("点击结算获取用户收货地址")
    @RequestMapping(value = "/generateOrder", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<ShippingAddress>> generateOrder(HttpServletRequest request) {
        SysUser user = getUser(request);
        Example<ShippingAddress> example = ShippingAddress.createExample();
        example.createCriteria().andEqualTo("userId", user.getUserId());
        List<ShippingAddress> sList = addressService.findListByExample(example);
        return ReturnResult.success(sList);
    }

    @ApiOperation("根据用户获取买家订单列表")
    @RequestMapping(value = "/productOrderList")
    @ResponseBody
    public ReturnResult<Page<ServiceProductOrderVo>> productOrderList(ServiceOrderVo serviceOrderVo, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,HttpServletRequest request){
        SysUser sysUser = getUser(request);
        serviceOrderVo.setUserId(sysUser.getUserId());
        Page<ServiceProductOrderVo> page = orderService.findPageOrderList(serviceOrderVo,pageNo,pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("根据商家获取订单列表")
    @RequestMapping(value = "/sellProductOrderList")
    @ResponseBody
    public ReturnResult<Page<ServiceOrderItemVo>> sellProductOrderList(ServiceOrderItemVo serviceOrderItemVo, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,HttpServletRequest request){
        SysUser sysUser = getUser(request);
        serviceOrderItemVo.setBusTeamId(sysUser.getBusTeamId());
        Page<ServiceOrderItemVo> page = orderService.findPageOrderSellList(serviceOrderItemVo,pageNo,pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("根据orderId获取订单详情")
    @RequestMapping(value = "/productOrderDetail")
    @ResponseBody
    public ReturnResult<ServiceProductOrderVo> productOrderDetail(@RequestParam(value = "orderId") Long orderId,HttpServletRequest request){
        ServiceProductOrderVo serviceProductOrderVo = orderService.findPageOrderDetail(orderId);
        return ReturnResult.success(serviceProductOrderVo);
    }

    @ApiOperation("根据orderItemId获取商品详情")
    @RequestMapping(value = "/orderItemDetail")
    @ResponseBody
    public ReturnResult<ServiceOrderItemVo> orderItemDetail(@RequestParam(value = "orderItemId") Long orderItemId,HttpServletRequest request){
        ServiceOrderItemVo vo = orderItemService.findOrderItemDetailById(orderItemId);
        return ReturnResult.success(vo);
    }

    @ApiOperation("根据orderItem和status修改orderItem状态")
    @RequestMapping(value = "/updateOrderItemStatus", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> updateOrderItemStatus(@RequestParam(value = "orderItemId") Long orderItemId, @RequestParam(value = "itemStatus") Integer itemStatus) {
        orderItemService.changeOrderItemStatus(orderItemId, itemStatus);
        return ReturnResult.success("ok");
    }


    @ApiOperation("添加或修改收货地址")
    @RequestMapping(value = "/saveOrUpdateShippingAddress")
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateShippingAddress(ShippingAddress shippingAddress, HttpServletRequest request){
        SysUser sysUser = getUser(request);
        if(sysUser!=null){
        shippingAddress.setUserId(sysUser.getUserId());
        shippingAddress.setUserName(sysUser.getRealName());
        addressService.saveOrUpdateAddress(shippingAddress);
        return ReturnResult.success("ok");
        }else {
            return ReturnResult.fail("账号出错");
        }
    }

    @ApiOperation("根据id获取收货地址")
    @RequestMapping(value = "/shippingAddressById")
    @ResponseBody
    public ReturnResult<ShippingAddress> shippingAddressById(@RequestParam(value = "addressId") Long addressId){
        ShippingAddress address = addressService.findById(addressId);
        return ReturnResult.success(address);
    }

    @ApiOperation("买家收货地址列表")
    @RequestMapping(value = "/shippingAddressList")
    @ResponseBody
    public ReturnResult<List<ShippingAddress>> shippingAddressList(ShippingAddress shippingAddress,HttpServletRequest request){
        SysUser sysUser = getUser(request);
        List<ShippingAddress> list = addressService.findAddressListByUserId(sysUser.getUserId());
        return ReturnResult.success(list);
    }

    @ApiOperation("删除收货地址")
    @RequestMapping(value = "/delShippingAddress")
    @ResponseBody
    public ReturnResult<Void> delShippingAddress(@RequestParam(value = "addressId") Long addressId){
        addressService.deleteById(addressId);
        return ReturnResult.success("ok");
    }


    @ApiOperation("服务商家列表")
    @RequestMapping(value = "/merchantList")
    @ResponseBody
    public ReturnResult<Page<ServiceMerchant>> merchantList(ServiceMerchantVo serviceMerchant,@RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize, HttpServletRequest request){
        Page<ServiceMerchant> page = merchantService.findMerchantPage(serviceMerchant,pageNo,pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("服务商家详情")
    @RequestMapping(value = "/merchantDetail")
    @ResponseBody
    public ReturnResult<ServiceMerchant> merchantDetail(@RequestParam(value = "merchantId") Long merchantId){
        ServiceMerchant serviceMerchant = merchantService.findById(merchantId);
        return ReturnResult.success(serviceMerchant);
    }

    @ApiOperation("审核服务商家注册信息")
    @RequestMapping(value = "/changeMerchant")
    @ResponseBody
    public ReturnResult<Void> changeMerchant(ServiceMerchantVo serviceMerchant){
        merchantService.saveOrUpdate(serviceMerchant);
        return ReturnResult.success("ok");
    }

    @ApiOperation("发货")
    @RequestMapping(value = "sendGoods", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> sendGoods(HttpServletRequest request, @RequestParam(value = "orderId") Long orderId) {
        orderItemService.sendGoods(orderId);
        return ReturnResult.success("ok");
    }

    @ApiOperation("交易成功")
    @RequestMapping(value = "/tradeSuccess", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> tradeSuccess(@RequestParam(value = "orderId") Long orderId, HttpServletRequest request) {
        orderItemService.tradeSuccess(orderId);
        return ReturnResult.success("ok");
    }

    @ApiOperation("商品/服务/租赁追加评论")
    @RequestMapping(value = "/addProductComment", method = RequestMethod.POST)
    @ResponseBody
    public ReturnResult<Void> addProductComment(HttpServletRequest request, ServiceCommentVo commentVo) {
        SysUser user = getUser(request);
        commentService.addComment(commentVo, user);
        return ReturnResult.success("ok");
    }

    @ApiOperation("评论列表")
    @RequestMapping(value = "/commentList",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<ServiceComment>> commentList(@RequestParam(value = "productId") Long productId,@RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        Page<ServiceComment> page = commentService.findPageComment(productId,pageNo,pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("校区教室")
    @RequestMapping(value = "/roomListOfCampus", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<Room>> roomListOfCampus(@RequestParam(value = "campusId") Long campusId, HttpServletRequest request) {
        SysUser user = getUser(request);
        List<Room> roomList = roomService.findListByCampusId(campusId,user.getAreaTeamId(),user.getBusTeamId());
        return ReturnResult.success(roomList);
    }

    @ApiOperation("已租赁教室列表")
    @RequestMapping(value = "/rentRoomList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<Room>> rentRentRoomList(HttpServletRequest request) {
        SysUser user = getUser(request);
        List<Long> roomIds = orderItemService.findRoomIds(user.getBusTeamId());
        List<Room> roomList = new ArrayList<>();
        //TODO:暂时注释，
//        if (roomIds.size()!=0) roomService.findByIds(roomIds);
        roomList = roomService.findListByExample(Room.createExample());
        return ReturnResult.success(roomList);
    }

}
